const { baseUrl, remove, getViews, getLayouts } = require("./utils.js");
const path = require("path");

exports.refreshLayouts = () => {
  const layoutsPath = path.join(baseUrl, "router/layouts.js");
  remove(layoutsPath);
  const layouts = getLayouts();
  const layoutActions = [
    {
      type: "add",
      path: `src/router/layouts.js`,
      templateFile: "templates/router/empty.hbs"
    }
  ];

  for (const layoutName in layouts) {
    const layout = layouts[layoutName];
    const path = `/${layoutName}`;
    layoutActions.push({
      type: "append",
      path: `src/router/layouts.js`,
      templateFile: "templates/router/append.hbs",
      data: {
        ...layout,
        path,
        isLayout: true
      }
    });
  }

  return layoutActions;
};

exports.refreshViews = () => {
  const viewsPath = path.join(baseUrl, "router/views.js");
  remove(viewsPath);
  const views = getViews();
  const viewActions = [
    {
      type: "add",
      path: `src/router/views.js`,
      templateFile: "templates/router/empty.hbs"
    }
  ];
  for (const viewName in views) {
    const view = views[viewName];
    const path = `${view.layout ? `/${view.layout}` : ""}/${viewName}`;
    viewActions.push({
      type: "append",
      path: `src/router/views.js`,
      templateFile: "templates/router/append.hbs",
      data: {
        ...view,
        path,
        isView: true
      }
    });
  }
  return viewActions;
};
